/*
 * @Description: 
 * @Autor: 赵子惠
 * @Date: 2021-07-06 17:03:21
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-04 14:39:12
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { } from '@shared/service-proxies/service-proxies';

import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
import { CalendarSettingServiceProxy,CalendarSettingDto } from '@shared/service-proxies/service-proxies';
import {CalendarModule} from 'primeng/calendar';
import * as moment from 'moment';

@Component({
  selector: 'app-time-update',
  templateUrl: './time-update.component.html',
  styleUrls: ['./time-update.component.less']
})
export class TimeUpdateComponent extends AppComponentBase {
     //引入子组件
 @ViewChild('createOrEditModal') modal: ModalDirective;
 @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
 @Input() editData: any;  //行数据
 @Input() title: string;  //标题
    form = {
        calendarName: undefined,
        workArrangement:true,
        startDate:undefined,
        endDate: undefined
    };
    value: Array<Date>;
       //日期显示中文
       chinese = {
        /** 每周第一天，0代表周日 */
        firstDayOfWeek: 0,
        /** 每周天数正常样式 */
        dayNames: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
        /** 每周天数短样式（位置较小时显示） */
        dayNamesShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
        /** 每周天数最小样式 */
        dayNamesMin: ["日", "一", "二", "三", "四", "五", "六"],
        /** 每月月份正常样式 */
        monthNames: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        /** 每月月份短样式 */
        monthNamesShort: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]
    }
    constructor(
        injector: Injector,
        private _calendarSettingServiceProxy: CalendarSettingServiceProxy,
        private _messageService: MessageService,
    ) {
        super(injector);
   }

  ngOnInit() {
  }
      //查看新增任务
    show() {
        if (this.title == '新增日历设置') {
            this.close();
        }
        this.modal.show();
    }
    save(){
        //获取实体
        let input = new CalendarSettingDto();
        if (this.title == '修改日历设置') {
            input.id = this.editData.id;
        }
        input.calendarName=this.form.calendarName;
        input.startDate=this.dateFormat(this.form.startDate);
        input.endDate=this.dateFormat(this.form.endDate);
        if (this.form.workArrangement == true) {
            input.workArrangement= 1;
        } else {
            input.workArrangement= 0;
        }
        this._calendarSettingServiceProxy.createOrUpdateCalendarSetting(input)
        .pipe(finalize(() => {  }))
        .subscribe(() => {
            this._messageService.success('保存成功');
            this.close();
            this.modalSave.emit(null);
        });
    }
    onShown(){
        if (this.title == '修改日历设置') {
            this.form.calendarName=this.editData.calendarName;
            this.form.startDate=new Date(this.editData.startDate);
            this.form.endDate=new Date(this.editData.endDate);
            if (this.editData.workArrangement == 1) {
                this.form.workArrangement = true;
            } else {
                this.form.workArrangement = false;
            }
        }
    }
    close(){
        this.modal.hide();
    }
      //日期格式化
      dateFormat(str): string {
        let date = new Date(str)
        let year: any = date.getFullYear()
        let month: any = date.getMonth() + 1
        if (month < 10) {
            month = '0' + month
        }
        let day: any = date.getDate()
        if (day < 10) {
            day = '0' + day;
        }
        return year + '-' + month + '-' + day
    }
}
